package com.jasonzqshen.ifm.featureModelCore.shapes;

import org.w3c.dom.Element;

public class FMRectangle {
    private static final String XML_WIDTH = "Width";

    private static final String XML_HEIGHT = "height";

    private double width;

    private double height;

    private FMPoint center;

    public FMRectangle() {
        width = -1;
        height = -1;
        center = new FMPoint();
    }

    public FMRectangle(double width, double height, FMPoint center) {
        this.width = width;
        this.height = height;
        this.center = center;
    }

    public void toXElem(Element xelem) {
        xelem.setAttribute(XML_WIDTH, ((Double) width).toString());
        xelem.setAttribute(XML_HEIGHT, ((Double) height).toString());
        this.center.toXElem(xelem);
    }

    public static FMRectangle parseXElem(Element xelem) {
        FMPoint center = FMPoint.parseXElem(xelem);
        double width = -1;
        double height = -1;

        String widthStr = xelem.getAttribute(XML_WIDTH);
        String heightStr = xelem.getAttribute(XML_HEIGHT);

        try {
            width = Double.parseDouble(widthStr);
            height = Double.parseDouble(heightStr);
        } catch (NumberFormatException e) {
            return new FMRectangle(-1, -1, center);
        }
        return new FMRectangle(width, height, center);
    }

    public double getWidth() {
        return this.width;
    }

    public double getHeight() {
        return this.height;
    }

    public FMPoint getCenter() {
        return this.center;
    }
}
